package clue

import (
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	clueDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/clue"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/clue"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type ClueManageController struct {
	controller.BaseController
	clueService clue.ClueServiceIface
}

// NewClueManageController 线索管理
func NewClueManageController(
	clueService clue.ClueServiceIface,
) *ClueManageController {
	return &ClueManageController{
		clueService: clueService,
	}
}

// RegisterRouter
// 注册路由
func (c *ClueManageController) RegisterRouter(g *gin.RouterGroup) {
	// 联系人跟进接口
	g.POST("add-clue-source-follow", c.addClueSourceFollow)

	g.POST("list", c.list)              // 线索列表接口
	g.GET("detail", c.detail)           // 线索详情接口∂
	g.POST("save", c.save)              // 新增/编辑线索接口
	g.POST("delete", c.delete)          // 删除线索
	g.GET("my-clue-stat", c.myClueStat) // 我的线索数量统计接口

	// 线索跟进人
	g.GET("tracker-list", c.trackerList)                      //跟进人列表
	g.POST("tracker-save", c.trackerSave)                     //保存跟进人
	g.POST("tracker-delete", c.trackerDelete)                 //删除跟进人
	g.POST("change-in-charge-person", c.changeInChargePerson) //修改线索负责人
	g.GET("in-charge-users", c.inChargeUsers)                 //ai外呼的线索负责人

	// 线索联系人
	g.GET("contact-list", c.contactList)      //联系人列表
	g.POST("contact-save", c.contactSave)     //保存联系人
	g.POST("contact-delete", c.contactDelete) //删除联系人
	g.GET("contact-detail", c.contactDetail)  //联系人详情

	// 线索跟进记录
	g.GET("follow-record-list", c.followRecordList) //跟进记录列表
	g.POST("add-follow-record", c.addFollowRecord)  //新增跟进记录
}

func (c *ClueManageController) addClueSourceFollow(ctx *gin.Context) {
	var request clueDto.AddClueSourceFollowRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.AddClueSourceFollow(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http addClueSourceFollow request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) empty(ctx *gin.Context) {
	c.Success(ctx, "i am empty")
}

func (c *ClueManageController) list(ctx *gin.Context) {
	var request clueDto.GetClueListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}

	resp, err := c.clueService.GetClueList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue list request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) detail(ctx *gin.Context) {
	var request clueDto.GetClueDetailByIdRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.GetClueDetailById(ctx, request.Id)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue detail request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) save(ctx *gin.Context) {
	var request clueDto.SaveClueRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Status == 0 {
		request.Status = 1 //默认为未处理
	}

	resp, err := c.clueService.SaveClue(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue save request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) delete(ctx *gin.Context) {
	var request clueDto.GetClueDetailByIdRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.DeleteClue(ctx, request.Id)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue delete request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) trackerList(ctx *gin.Context) {
	var request clueDto.GetClueTrackerListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}

	resp, err := c.clueService.GetClueTrackerList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue trackerList request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) trackerSave(ctx *gin.Context) {
	var request clueDto.SaveClueTrackerRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.SaveClueTracker(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue trackerSave request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) trackerDelete(ctx *gin.Context) {
	var request clueDto.DeleteClueTrackerRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.DeleteClueTracker(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue trackerDelete request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) changeInChargePerson(ctx *gin.Context) {
	var request clueDto.ReplaceChargeUserRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.ReplaceChargeUser(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue changeInChargePerson request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) contactList(ctx *gin.Context) {
	var request clueDto.GetClueContactListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}

	resp, err := c.clueService.GetClueContactList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue contactList request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) contactSave(ctx *gin.Context) {
	var request clueDto.SaveClueContactRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.SaveClueContact(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue contactSave request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) contactDelete(ctx *gin.Context) {
	var request clueDto.DeleteClueContactRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.DeleteClueContact(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue contactDelete request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) contactDetail(ctx *gin.Context) {
	var request clueDto.GetClueContactDetailByIdRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.GetClueContactDetailById(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue GetClueContactDetailByIdRequest request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) followRecordList(ctx *gin.Context) {
	var request clueDto.GetClueFollowRecordListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}
	resp, err := c.clueService.GetClueFollowRecordList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue followRecordList request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) addFollowRecord(ctx *gin.Context) {
	var request clueDto.AddClueFollowRecordRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.AddClueFollowRecord(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue addFollowRecord request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) inChargeUsers(ctx *gin.Context) {
	resp, err := c.clueService.GetInChargeUserList(ctx)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue inChargeUserList err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, resp)
	return
}

func (c *ClueManageController) exportClueList(ctx *gin.Context) {
	var request clueDto.ExportClueListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	resp, err := c.clueService.ExportClueList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "clue exportClueList request:%+v, err:%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}

	c.Success(ctx, resp)
}

func (c *ClueManageController) myClueStat(ctx *gin.Context) {
	result, err := c.clueService.MyClueStat(ctx)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result)
}
